package com.css.ids.server.util;

import java.util.List;

import com.css.apps.base.common.provider.UserQueryProvider;
import com.css.apps.base.menu.MenuFactory;
import com.css.core.configuration.Environment;
import com.css.core.model.IUser;
import com.css.db.config.ConfigurationManager;
import com.css.util.StringHelper;
import com.css.webbase.WebBaseUtil;
import com.css.webbase.menu.MenuItem;
import com.css.webbase.webwork.WebworkUtil;

public class MenuUtil {
	// 缓存内容
	private static String cacheName = "_user_menu_content_cache/";

	/**
	 * 获取用户菜单项
	 * 
	 * @return
	 */
	public static String getUserMenuContent() {
		String menuContent = getCachedMenuContent();
		if (menuContent != null)
			return menuContent;
		List<MenuItem> menus = getUserMenuContent(WebBaseUtil.getCurrentUser(),Environment.DEFAULT_SYS) ;
//		List<MenuItem> menus = MenuFactory.getUserMenu();
		StringBuffer buf = new StringBuffer();
		for (MenuItem mi : menus) {
			appendMenuItem(mi, buf, true);
		}
		menuContent = buf.toString();
		cacheMenuContent(menuContent);
		return menuContent;
	}

	public static List<MenuItem> getUserMenuContent(IUser user,String sysId) {
		List<MenuItem> menus = MenuFactory.getUserMenu(user,sysId);
		return menus;
	}

	private static void appendMenuItem(MenuItem item, StringBuffer menuStr,
			boolean iconFlag) {
		if (item == null)
			return;
		menuStr.append("<li>");
		String url = item.getPath();
		String title = item.getName();
		if (StringHelper.isEmptyByTrim(url)) {
			menuStr.append("<a href=\"javascript:;\">");
		} else {
			menuStr.append("<a href=\"").append(url.trim()).append("\" rel=\"")
					.append(item.getFuncode())
					.append("\" target=\"cssTab\"	title=\"").append(title)
					.append("\" refresh=\"true\">");
		}
		if (iconFlag) {
			String icon = item.getIcon();
			menuStr.append("<i class=\"").append(icon != null ? icon : "")
					.append("\"></i>");
		}
		menuStr.append("<span class=\"title\">")
				.append(title != null ? title : "").append("</span>");
		List<MenuItem> items = item.getMenus();
		if (items != null && items.size() > 0)
			menuStr.append("<span class=\"arrow\"></span>");
		menuStr.append("</a>");
		if (items != null && items.size() > 0) {
			menuStr.append("<ul  class=\"sub-menu\">");
			for (MenuItem mi : items) {
				appendMenuItem(mi, menuStr, false);
			}
			menuStr.append("</ul>");
		}
		menuStr.append("</li>");
	}

	/**
	 * 从缓存中获取用户菜单
	 * 
	 * @return
	 */
	private static String getCachedMenuContent() {
		IUser user = UserQueryProvider.currentUser();
		if (user == null)
			return null;
		return (String) com.css.db.memcached.MemCachedFactory
				.get(getUserKey(user));
	}

	/**
	 * 缓存用户菜单
	 * 
	 * @return
	 */
	private static void cacheMenuContent(String content) {
		if (content == null)
			return;
		IUser user = UserQueryProvider.currentUser();
		if (user == null)
			return;
		com.css.db.memcached.MemCachedFactory.set(getUserKey(user), content,
				ConfigurationManager.getCacheTime());
	}

	/**
	 * 返回用户缓存键值
	 * 
	 * @param user
	 * @return
	 */
	public static String getUserKey(IUser user) {
		return cacheName + "/" + user.getOrganId() + "/" + user.getUserId();
	}

	/**
	 * 刷新用户菜单
	 */
	public static void refreshMenu() {
		IUser user = UserQueryProvider.currentUser();
		if (user == null)
			return;
		// 清除菜单缓存
		com.css.db.memcached.MemCachedFactory.delete(cacheName
				+ getUserKey(user));
		WebworkUtil.getActionContext().getSession()
				.remove(com.css.webbase.menu.MenuFactory.class.getName());
		// 装载菜单
		com.css.webbase.menu.MenuFactory.getUserMenu();
	}
}